package Vue;

import java.awt.Adjustable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import Controller.CtrlMonde;
import Modele.Case;
import Modele.Position;

public class FenModifTerrainListener implements AdjustmentListener,
		ActionListener, ItemListener {
	
	private FenModifTerrain fmt;
	
	private Position pos;
	
	public FenModifTerrainListener(FenModifTerrain fmt, Position p) {
		this.fmt = fmt;
		this.pos = p;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getActionCommand().equals("Obstacle"))
		{
			if (fmt.getJcb().isSelected() == true)
			{
				fmt.getJsb().setValue(fmt.getJsb().getMaximum());
			}
			else
			{
				fmt.getJsb().setValue(0);
			}
			fmt.ctrl_appli.refreshFenetres();
			//fmt.refresh();
			//fmt.repaint();
		}
		else
		{
			if (arg0.getActionCommand() == "Annuler")
			{
				fmt.setVisible(false);
			}
			else
			{
				if (arg0.getActionCommand().equals("Valider"))
				{
					int value = fmt.getJsb().getValue();
					boolean trouve = false;
					for (CasePosBean casePB : CtrlMonde.getListeCases())
					{
						if (casePB.getP() == pos)
						{
							casePB.getC().setNiveauTerrain(value);
							trouve = true;
						}
					}
					if (trouve == false)
					{
						Position p = pos;
						Case c = new Case(value, 0, null, 0, null);
						CasePosBean cpb = new CasePosBean(c, p);
						fmt.ctrl_appli.getCtrl_monde().ajouter(cpb);
					}					
					fmt.ctrl_appli.refreshFenetres();
					fmt.setVisible(false);
				}
			}
		}

	}

	@Override
	public void adjustmentValueChanged(AdjustmentEvent arg0) {
		
		if (fmt.getJsb().getValue() != fmt.getJsb().getMaximum())
		{
			fmt.getJcb().setSelected(false);
		}
		
		Adjustable source = arg0.getAdjustable();
	    if (arg0.getValueIsAdjusting()) {
	      return;
	    }
	    int orient = source.getOrientation();
	    if (orient == Adjustable.HORIZONTAL) {
	      System.out.println("from horizontal scrollbar"); 
	    } else {
	      System.out.println("from vertical scrollbar");
	    }
	    int type = arg0.getAdjustmentType();
	    switch (type) {
	    case AdjustmentEvent.UNIT_INCREMENT:
	      fmt.getAffDiff().setText("Scrollbar was increased by one unit");
	      break;
	    case AdjustmentEvent.UNIT_DECREMENT:
	    	fmt.getAffDiff().setText("Scrollbar was decreased by one unit");
	      break;
	    case AdjustmentEvent.BLOCK_INCREMENT:
	    	fmt.getAffDiff().setText("Scrollbar was increased by one block");
	      break;
	    case AdjustmentEvent.BLOCK_DECREMENT:
	    	fmt.getAffDiff().setText("Scrollbar was decreased by one block");
	      break;
	    case AdjustmentEvent.TRACK:
	    	fmt.getAffDiff().setText(String.valueOf(fmt.getJsb().getValue()));
	      break;
	    }
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		fmt.getJsb().setValue(fmt.getJsb().getMaximum());
		fmt.repaint();
		fmt.refresh();
		fmt.ctrl_appli.refreshFenetres();
	}

}
